package com.example.demo.controller;

import com.example.demo.common.utills.CommonResult;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@CrossOrigin(allowCredentials = "true")
public class UserController {
    @Resource
    UserService userService;

    @Resource
    UserMapper userMapper;

    @RequestMapping("/add")
    public CommonResult addUser(User user){
        return userService.addUser(user);
    }

    @RequestMapping("/query")
    public CommonResult queryUser(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                @RequestParam(value = "pageSize",required = false,defaultValue = "10")int pageSize,
                                  @RequestParam(value = "username",required = false,defaultValue = "")String  username,
                                  @RequestParam(value = "uid",required = false,defaultValue = "0")int  uid){
        return userService.selectAllUser(pageNum, pageSize,username,uid);
    }

    @RequestMapping("/queryUserEx")
    public CommonResult queryUserEx(String account){
        User user = userMapper.selectUserEx(account);
        if(user==null){
            return new CommonResult(200,"该账户可用");
        }
        return new CommonResult(500,"该账户已注册");
    }

    @RequestMapping("/delete")
    public CommonResult deleteUser(int uid){
        return userService.deleteUser(uid);
    }

    @RequestMapping("/reset")
    public CommonResult resetPsd(String psd,String account){
        return userService.resetPsd(psd, account);
    }

    @RequestMapping("/sendMes")
    public CommonResult sendMes(String account,String phone) throws Exception {
        return userService.sendMes(account,phone);
    }

    @RequestMapping("/forgetPsd")
    public CommonResult forgetPsd(String account,String phone,String code){
        return userService.forgetPsd(account,phone,code);
    }

    @RequestMapping("/updateType")
    public CommonResult updateUserType(String type,int uid) {
        userMapper.updateUserType(type,uid);
        return new CommonResult(200,"修改成功");
    }
}
